package com.ophiux.wechat.utils;

import java.io.Writer;

import com.ophiux.wechat.cmnative.model.Native1CallBackParamModel;
import com.ophiux.wechat.test.xml;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class XmlUtil_beta {

	/**
	 * 创建Xstream实例
	 */
	private static XStream xstream = new XStream(new XppDriver(){
		
		public HierarchicalStreamWriter createWriter(Writer out){
			return new PrettyPrintWriter(out){
				//对所有xml节点都增加CDATA标记
				boolean cdata = true;
				
				public void startNode(String name,Class clz){
					super.startNode(name,clz);
				}
				
				protected void writeText(QuickWriter writer,String text){
					if(cdata){
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					}else{
						writer.write(text);
					}
				}
				
			};
			
		}
	});
	
	
	/**
	 * 将xml字符串转换为对象
	 */
	public static xml fromXml(String xmlStr){
		xstream.alias("xml",xml.class);
		xml model = (xml) xstream.fromXML(xmlStr);
		return model;
	}
	
}
